class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {

        vector<vector<int>> vecRet;
        sort(nums.begin(),nums.end());
        for(int i=0;i<static_cast<int>(nums.size());++i){
            if(nums[i]>0){
                break;
            }
            if(i>0 && nums[i]==nums[i-1]){
                continue;
            }
            int left=i+1,right=nums.size()-1;
            while(left<right){
                int t=nums[i]+nums[left]+nums[right];
                if(t>0){
                    --right;
                }
                else if(t<0){
                    ++left;
                }
                else{
                    vecRet.push_back({nums[i],nums[left],nums[right]});
                    ++left;
                    while(left<right && nums[left]==nums[left-1]){
                        ++left;
                    }
                }
            }
        }
        
        return vecRet;
    }
};
